package com.zhiqianbao.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.zhiqianbao.R;
import com.zhiqianbao.utils.MyAlertDialog;
import com.zhiqianbao.utils.PermissionDialog;

/**
 * Created by liang on 2016/1/27.
 */
public class BaseFragment extends Fragment {

    Dialog waitDialog;

    private PermissionDialog permissionDialog;


    protected void showWaitDialog() {
        waitDialog = new Dialog(getActivity(), R.style.DialogStyle);
//        LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View waitView = LayoutInflater.from(getActivity()).inflate(R.layout.loading_dialog, null);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        params.alpha = 0.0F;
        waitDialog.setContentView(waitView, params);
        waitDialog.show();
    }

    protected void closeWaitDialog() {
        if(null != waitDialog) {
            waitDialog.cancel();
            waitDialog.dismiss();
        }
    }

    /**
     *
     * @param title
     * @param positive
     * @param negative
     */
    public void showAlertMsgDialog(String title, String positive, String negative) {
        MyAlertDialog alertDialog = new MyAlertDialog(getActivity()).builder().setTitle(title).setPositiveButton(positive, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                startActivity(new Intent(getActivity(), ProcessActivity.class));
            }
        }).setNegativeButton(negative, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        alertDialog.show();
    }


    /**
     * 带输入框的AlertDialog
     */
    public void showAlertDialog(){
        final AlertDialog dialog = new AlertDialog.Builder(getActivity()).create();
        dialog.setView(LayoutInflater.from(getActivity()).inflate(R.layout.alert_dialog, null));
        dialog.show();
        dialog.getWindow().setContentView(R.layout.alert_dialog);
        Button btnPositive = (Button) dialog.findViewById(R.id.bt_sure);
        Button btnNegative = (Button) dialog.findViewById(R.id.btn_cancel);
        final EditText etContent = (EditText) dialog.findViewById(R.id.et_getcode);
        btnPositive.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String str = etContent.getText().toString();
                if (isNullEmptyBlank(str)) {
                    etContent.setError("输入内如不能为空");
                } else {
                    dialog.dismiss();
                    Toast.makeText(getActivity(), etContent.getText().toString(), Toast.LENGTH_LONG).show();
                }
            }
        });
        btnNegative.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
    }

    private static boolean isNullEmptyBlank(String str) {
        if (str == null || "".equals(str) || "".equals(str.trim()))
            return true;
        return false;
    }


    private Activity mActivity;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        mActivity=activity;
    }
    //得到可靠地Activity
    public Activity getMyActivity()
    {
        return mActivity;
    }


    /**
     * 短时间吐司
     */
    Toast toastShort;
    public Toast showShortToast1(String message) {
        if (!TextUtils.isEmpty(message)) {
            if (toastShort == null) {
                if(getActivity()!=null) {
                    toastShort = Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT);
                }
                } else {
                toastShort.setText(message);
                toastShort.setDuration(Toast.LENGTH_SHORT);
            }
            toastShort.setGravity(Gravity.CENTER, 0, 0);
            toastShort.show();

        }
        return toastShort;
    }
//    protected void showShortToast(String msg) {
//        //Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
//        Log.i("======", "=====================baseFrament's Context :" + getActivity());
//        CustomDialog.Builder builder = new CustomDialog.Builder(getActivity());
//        builder.setMessage(msg);
//        builder.setTitle("提示");
//        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//                //设置你的操作事项
//            }
//        });
//        builder.create().show();
//    }
//
//
//    protected void showLongToast(String msg) {
//        //Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
//        Log.i("======", "=====================baseFrament's Context :" + getActivity());
//        Log.i("======", "=====================baseFrament's Context :" + getContext());
//        CustomDialog.Builder builder = new CustomDialog.Builder(getActivity());
//        builder.setMessage(msg);
//        builder.setTitle("提示");
//        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//                //设置你的操作事项
//            }
//        });
//        builder.create().show();
//    }
//
//    protected void showShortToast(String msg,final FragmentActivity context) {
//        //Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
//        Log.i("======", "=====================baseFrament's Context :" + context);
//        CustomDialog.Builder builder = new CustomDialog.Builder(context);
//        builder.setMessage(msg);
//        builder.setTitle("提示");
//        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//                //设置你的操作事项
//            }
//        });
//        builder.create().show();
//    }
//
//
//    protected void showLongToast(String msg,final FragmentActivity context) {
//        //Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
//        Log.i("======", "=====================baseFrament's Context :" + context);
//        CustomDialog.Builder builder = new CustomDialog.Builder(context);
//        builder.setMessage(msg);
//        builder.setTitle("提示");
//        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//                //设置你的操作事项
//            }
//        });
//        builder.create().show();
//    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
